/*
 * Created on 2004-08-04
 */
package org.opencube.oms.mapping.generator;

import info.fingo.util.Text;

import org.opencube.oms.meta.ComplexAttribute;
import org.opencube.oms.meta.Scheme;

/**
 * @author <a href="mailto:maciek@fingo.pl">Maciej Mroczko - FINGO</a>
 *
 * TODO comments
 */
public class PHPTemplateUtil
{
	static private String BASE_ENTITY = "Entity";
	static private String BASE_FINDER = "Finder";

	public String upCase( String s )
	{
		String result = s;
		if( s != null && s.trim().length() > 0 )
		{
			s = s.trim();
			result = s.substring( 0, 1 ).toUpperCase();
			if( s.length() > 0 )
			{
				result += s.substring( 1 );
			}
		}
		return result;
	}
	
	public String lowCase( String s )
	{
		String result = s;
		if( s != null && s.trim().length() > 0 )
		{
			s = s.trim();
			result = s.substring( 0, 1 ).toLowerCase();
			if( s.length() > 0 )
			{
				result += s.substring( 1 );
			}
		}
		return result;
	}
	
	
	public String getEntityExtends( String base, Scheme s )
	{
		String result = "Base" + upCase( s.getName() );
		return getClassPrefix( base, s ) + result;
	}
	
	public String getFinderExtends( String base, Scheme s )
	{
		String result = "Base" + upCase( s.getName() + "Finder");
		return getClassPrefix( base, s ) + result;
	}

	public String getBaseEntityExtends( String base, Scheme s )
	{
		String result = "";
		if( s.getParentScheme() == null )
		{
			result = getClassPrefix( base, null ) + BASE_ENTITY;
		}
		else
		{
			result = getClassPrefix( base, s.getParentScheme() ) + getEntityTypeNameBase( s.getParentScheme() );
		}
		
		return result;
	}

	public String getBaseFinderExtends( String base, Scheme s )
	{
		String result = "";
		if( s.getParentScheme() == null )
		{
			result = getClassPrefix( base, null ) + BASE_FINDER;
		}
		else
		{
			result = getClassPrefix( base, s.getParentScheme() ) + getFinderTypeNameBase( s.getParentScheme() );
		}
		
		return result;
	}

	public String getEntityTypeNameBase( Scheme s )
	{	
		return upCase( s.getName() );
	}
	
	public String getFinderTypeNameBase( Scheme s )
	{		
		return upCase( s.getName() ) + "Finder";
	}

	public String getConstant( String s )
	{
		String result = "";
		boolean lastLower = false;
		for (int i = 0; s != null && i < s.length(); i++)
		{
			char c = s.charAt( i );
			
			if( c >= 'A' && c <= 'Z')
			{
				if( lastLower )
				{
					result += "_";
				}
			}
			else
			{
				lastLower = true;
			}
			result += Character.toUpperCase( c );
		}
		return result;
	}
	
	public String getPackage( String base, Scheme s )
	{
		String result = (base == null ? "" : base ) + (s == null ? "" : s.getNamespace().getPath()); 
		result = Text.substitute( result, "-", "_" );
		if( result.startsWith( "/" ) )
		{
			result = result.substring( 1 );
		}
		
		return result;
	}
	
	public String getBaseEntityInclude( String base, Scheme s )
	{
		String result = "";
		if( s.getParentScheme() == null )
		{
			result = base + "/" + BASE_ENTITY;
		}
		else
		{
			result = getPackage( base, s.getParentScheme() ) + '/' + getEntityTypeNameBase( s.getParentScheme() );
		}
		
		return result;
	}
	
	public String getEntityInclude( String base, Scheme s )
	{
		return getPackage( base, s ) + "/Base" + getEntityTypeNameBase( s );
	}
	
	public String getBaseFinderInclude( String base, Scheme s )
	{
		String result = "";
		if( s.getParentScheme() == null )
		{
			result = base + "/" + BASE_FINDER;
		}
		else
		{
			result = getPackage( base, s.getParentScheme() ) + '/' + getFinderTypeNameBase( s.getParentScheme() );
		}
		
		return result;
	}
	
	public String getFinderInclude( String base, Scheme s )
	{
		return getPackage( base, s ) + "/Base" + getFinderTypeNameBase( s );
	}
	
	public String getAttributeEntityInclude( String base, ComplexAttribute a )
	{
		Scheme s = a.getTargetScheme();
		String result = getPackage( base, s ) + '/' + getEntityTypeNameBase( s );
		
		return result;
	}
	
	public String makeSingle( String str )
	{
		if( str.endsWith( "s" ))
		{
			return str.substring( 0, str.length() - 1 );
		}
		return str;
	}
	
	public String makeMultiple( String str )
	{
		if( str.endsWith( "s" ))
		{
			return str + "es";
		}
		return str +"s";
	}
	
	public String getClassPrefix( String base, Scheme s )
	{
		//return Text.substitute( getPackage( base, s ), "/", "_" ) + '_';
		if( s != null )
		{
			return s.getNamespace().getName() + '_';
		}
		return "";
	}
	
	public String getFinderTypeName( String base, Scheme s )
	{
		return getClassPrefix( base, s ) + getFinderTypeNameBase( s );
	}
	
	public String getEntityTypeName( String base, Scheme s )
	{
		return getClassPrefix( base, s ) + getEntityTypeNameBase( s );
	}
}
